package com.gzx.plugin.tjzy.modular.sms.batch.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.fastjson2.JSON;
import com.gzx.plugin.tjzy.modular.sms.batch.vo.GzxSmsExcelVo;
import org.apache.commons.lang3.StringUtils;
import vip.xiaonuo.common.listener.CommonExcelListener;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author zjt
 * @description
 * @date 2025/3/6 18:22
 */
public class SMSExcelListener extends CommonExcelListener<GzxSmsExcelVo> {

    @Override
    public void customAnalyse(GzxSmsExcelVo gzxSmsExcelVo, AnalysisContext analysisContext) {
        Map<Integer, Cell> cellMap = analysisContext.readRowHolder().getCellMap();
        int index = 0;
        Map<String, Object> map = new LinkedHashMap<>();
        for (Iterator<Integer> iterator = cellMap.keySet().iterator(); iterator.hasNext(); ) {
            Integer key = iterator.next();
            index++;
            if (index > 1) {
                ReadCellData<String> readCellData = (ReadCellData) cellMap.get(key);
                String value = StringUtils.isNotEmpty(readCellData.getStringValue()) ? readCellData.getStringValue() : String.valueOf(readCellData.getNumberValue());
                map.put("item" + (index - 1), value);
            }
        }
        gzxSmsExcelVo.setItemJson(JSON.toJSONString(map));
        add(gzxSmsExcelVo);
    }
}